import bot from "../model/bot/amesu";
import func from "../model/func";
import { EffectType, group } from "../shared/interface";
import { MsgAction, Tpl_battleLog } from "../shared/master/MsgAction";
import { style_default } from "../style/default";
export default async function (data: MsgAction) {
    let d = data.data as Tpl_battleLog;
    let end = d.end;
    func.roundNumbers(d)
    let s = new style_default();
    let nameMap: Map<string, string> = new Map();
    /**
     * 分析出 log 中 同一技能 的效果组
     */
    let groupA_logs: Map<string, { usename: string, skname: string, effect: { effect_type: EffectType, val: number | string }[] }[]> = new Map();
    let groupB_logs: Map<string, { usename: string, skname: string, effect: { effect_type: EffectType, val: number | string }[] }[]> = new Map();
    for (let i = 0; i < d.logs.length; i++) {
        let item = d.logs[i];
        let group_Map = groupA_logs;
        if (item.group == group.客场) {
            group_Map = groupB_logs;
        }
        nameMap.set(`${item.use_uuid}`, item.use_name);
        // _${item.group}
        let bodylogs = group_Map.get(item.use_name) || [];
        let skItem = bodylogs.find((e) => {
            return e.skname == item.sk_name
        });
        if (!skItem) {
            skItem = { usename: item.use_name, skname: item.sk_name, effect: [] }
            bodylogs.push(skItem)
        }
        const val = item.val
        if (typeof (val) == 'string') {
            skItem.effect.push({ effect_type: item.type, val: val });
        } else {
            let effect_idx = skItem.effect.findIndex((e) => {
                return e.effect_type == item.type
            })

            if (effect_idx == -1) {
                skItem.effect.push({ effect_type: item.type, val: val });
            } else {
                (skItem.effect[effect_idx].val as number) += val;
            }
        }
        group_Map.set(item.use_name, bodylogs)
    }

    if (groupA_logs.size) {
        s.setline_Title(`蓝方统计`, '🔵')
        groupA_logs.forEach((e, id) => {
            let str = `${id}`;
            for (let i = 0; i < e.length; i++) {
                const element = e[i];
                str += `│▌${element.skname}`;
                for (let j = 0; j < element.effect.length; j++) {
                    const effect_item = element.effect[j];
                    str += func.getEffectStyle(effect_item.effect_type, effect_item.val)
                }
            }
            s.addLine(str)
        });
    } else {
        s.addLine(`💤蓝方本回合没有任何动作`)
    }


    if (groupB_logs.size) {
        s.setline_Title(`红方统计`, '🔴')
        groupB_logs.forEach((e, id) => {
            let str = `${id}`;
            for (let i = 0; i < e.length; i++) {
                const element = e[i];
                str += `│▌${element.skname}`;
                for (let j = 0; j < element.effect.length; j++) {
                    const effect_item = element.effect[j];
                    str += func.getEffectStyle(effect_item.effect_type, effect_item.val)
                }
            }
            s.addLine(str)
        });
    } else {
        s.addLine(`💤红方本回合没有任何动作`)
    }
    if (d.census[0].num <= 0 || d.census[1].num <= 0) {
    } else {
        s.setline_Title(`双方状态`, '💞')
        for (let i = 0; i < d.census.length; i++) {
            let start = i ? '🔴' : '🔵'
            const element = d.census[i];
            if (element.num <= 0) {
                s.addLine(`${start}全军覆没`)
            } else if (element.num <= 0.8) {
                const line = Math.ceil(element.num * 100);
                let bar = '';
                for (let i = 0; i < line / 10; i++) {
                    bar += '▋'
                }
                s.addLine(`${start}${bar}${line}%`)
            } else {
                const line = Math.ceil(element.num * 100);
                let bar = '';
                for (let i = 0; i < line / 10; i++) {
                    bar += '▋'
                }
                s.addLine(`${start}${bar}`)
            }
        }

    }
    if (end) {
        s.setline_Title(`胜负已分`,'🎁')
        s.addLine(`${end.winGroup == group.主场 ? '🔵蓝方' : '🔴红方'}在第${end.round}回合获得胜利`);
        for (let i = 0; i < end.gifts.length; i++) {
            const element = end.gifts[i];
            let line = `${element.user_name}`;
            let info = element.info;
            if (info.exp) {
                line += `│▌⏳战斗经验+${func.BN(info.exp)}`
            }
            if (info.gold) {
                line += `│▌💰️金币+${func.BN(info.gold)}`
            }
            s.addLine(line)
        }
    }
   


    bot.sendText(data.tolocation, s.end());
}
